package com.example.hangzhou.welbeing.mytestdemoapp.adpater;

import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/4/18
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class HeaderAndFooterWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int BASE_ITEM_TYPE_HEADER = 100000;
    private static final int BASE_ITEM_TYPE_FOOTER = 200000;

    //头集合 尾结合
    private SparseArrayCompat<View> mHeaderViews = new SparseArrayCompat<>();
    private SparseArrayCompat<View> mFootViews = new SparseArrayCompat<>();
    private RecyclerView.Adapter mInnerAdapter;

    public HeaderAndFooterWrapper(RecyclerView.Adapter adapter) {
        this.mInnerAdapter = adapter;
    }

    private boolean isHeaderViewPos(int position) {
        return position < getHeadersCount();
    }
    private boolean isFooterViewPos(int position){
        return position > getHeadersCount()+ mInnerAdapter.getItemCount();
    }

    public int getHeadersCount() {
        return mHeaderViews.size();
    }
    public int getFootersCount(){
        return mFootViews.size();
    }

    public void addHeaderView(View view){
        mHeaderViews.put(mHeaderViews.size()+BASE_ITEM_TYPE_HEADER,view);
    }
    public void addFootView(View view){
        mFootViews.put(mFootViews.size()+BASE_ITEM_TYPE_FOOTER,view);
    }


    public int getRealItemCount(){
        return mInnerAdapter.getItemCount();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderViews.get(viewType) != null){
            return new HeaderViewHolder(mHeaderViews.get(viewType));
        }else if(mFootViews.get(viewType) != null){
            return new FootViewHolder(mFootViews.get(viewType));
        }
        return mInnerAdapter.onCreateViewHolder(parent,viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if(isHeaderViewPos(position)){
                return;
            }
            if(isFooterViewPos(position)){
                return;
            }
        mInnerAdapter.onBindViewHolder(holder,position - getHeadersCount());
    }

    @Override
    public int getItemCount() {
        return getHeadersCount()+getFootersCount()+getRealItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        if(isHeaderViewPos(position)){
            return mHeaderViews.keyAt(position);
        }else if(isFooterViewPos(position)){
            return mFootViews.keyAt(position - getHeadersCount() - getRealItemCount());
        }
        return mInnerAdapter.getItemViewType(position - getHeadersCount());
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
       // super.onAttachedToRecyclerView(recyclerView);
        mInnerAdapter.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager  =  recyclerView.getLayoutManager();
        if(layoutManager instanceof GridLayoutManager){
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int viewType = getItemViewType(position);
                    if(mHeaderViews.get(viewType) != null){
                        return gridLayoutManager.getSpanCount();
                    }else if(mFootViews.get(viewType) != null){
                        return gridLayoutManager.getSpanCount();
                    }else{
                        return 1;
                    }
                }
            });

        }
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {
        HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }

    private class FootViewHolder extends RecyclerView.ViewHolder {
        FootViewHolder(View itemView) {
            super(itemView);
        }
    }
}
